/**
 * 全局扩展
 */
/**
 * ip转数字
 * @param ip
 * @returns {number}
 * @private
 */
global._ip2int = function (ip) {
    var num = 0;
    ip = ip.split('.');
    num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]);
    num = num >>> 0;
    return num;
};
/**
 * 数字转ip
 * @param num
 * @returns {string|*}
 * @private
 */
global._int2iP = function (num) {
    var str;
    var tt = [];
    tt[0] = num >>> 24 >>> 0;
    tt[1] = num << 8 >>> 24 >>> 0;
    tt[2] = num << 16 >>> 24;
    tt[3] = num << 24 >>> 24;
    str = String(tt[0]) + '.' + String(tt[1]) + '.' + String(tt[2]) + '.' + String(tt[3]);
    return str;
};

/**
 * 排序函数
 */
function sort_node(v, w) {
    return v['sort'] - w['sort'];
}

function sort_node1(v, w) {
    return w['sort'] - v['sort'];
}

/**
 * global get_children
 * 获取子集分类 （这里是获取所有子集）
 */
global.get_children = function (nodes, parent, sn = 0) {
    // console.log(11);
    var children = [];
    var last = [];
    /* 未访问的节点 */
    /*
       * 获取根分类列表。
       * 创建一个虚拟父级分类亦可
       **/
    var node = null;
    for (var i in nodes) {
        node = nodes[i];
        if (node['pid'] == parent) {
            node['deep'] = 0;
            children.push(node);
        } else {
            last.push(node);
        }
    }
    if (sn == 0) {
        children.sort(sort_node);
    } else {
        children.sort(sort_node1);
    }

    /* 同级排序 */
    var jumper = 0;
    var stack = children.slice(0);
    /* easy clone */

    while (stack.length > 0 &&
    /* just in case */jumper++ < 1000) {
        var shift_node = stack.shift();
        var list = [];
        /* 当前子节点列表 */
        var last_static = last.slice(0);
        last = [];
        for (var i in last_static) {
            node = last_static[i];
            if (node['pid'] == shift_node['id']) {
                node['deep'] = shift_node['deep'] + 1;
                list.push(node);
            } else {
                last.push(node);
            }
        }
        if (sn == 0) {
            list.sort(sort_node);
        } else {
            list.sort(sort_node1);
        }

        for (var i in list) {
            node = list[i];
            stack.push(node);
            children.push(node);
        }
    }
    /*
       * 有序树非递归前序遍历
       *
       * */
    var stack = [];
    /* 前序操作栈 - 分类编号 */
    var top = null;
    /* 操作栈顶 */
    var tree = children.slice(0);
    /* 未在前序操作栈内弹出的节点 */
    var has_child = false;
    /* 是否有子节点，如无子节点则弹出栈顶 */
    var children = [];
    /* 清空结果集 */
    var jumper = 0;
    last = [];
    /* 未遍历的节点 */
    var current = null;
    /* 当前节点 */
    stack.push(parent);
    /* 建立根节点 */

    while (stack.length > 0) {
        if (jumper++ > 1000) {
            break;
        }
        top = stack[stack.length - 1];
        has_child = false;
        last = [];

        for (var i in tree) {
            current = tree[i];
            if (current['pid'] == top) {
                top = current['id'];
                stack.push(top);
                children.push(current);
                has_child = true;
            } else {
                last.push(current);
            }
        }
        tree = last.slice(0);
        if (!has_child) {
            stack.pop();
            top = stack[stack.length - 1];
        }
    }
    return children;
};
/**
 * obj_values(obj);
 * 获取对象中的所有的值，并返回数组
 * @param obj
 * @returns {Array}
 */
/* global obj_values */
global.obj_values = function (obj) {
    const objkey = Object.keys(obj);
    const objarr = [];
    objkey.forEach(key => {
        objarr.push(obj[key]);
    });
    return objarr;
};
/**
 * 判断对象是否相等
 * @param a
 * @param b
 * @returns {boolean}
 */
/* global isObjectValueEqual */
global.isObjectValueEqual = function (a, b) {
    // Of course, we can do it use for in
    // Create arrays of property names
    var aProps = Object.getOwnPropertyNames(a);
    var bProps = Object.getOwnPropertyNames(b);

    // If number of properties is different,
    // objects are not equivalent
    if (aProps.length != bProps.length) {
        return false;
    }

    for (var i = 0; i < aProps.length; i++) {
        var propName = aProps[i];

        // If values of same property are not equal,
        // objects are not equivalent
        if (a[propName] !== b[propName]) {
            return false;
        }
    }

    // If we made it this far, objects
    // are considered equivalent
    return true;
};
/**
 * trim()
 * @param str [删除左右两端的空格]
 * @returns {*|void|string|XML}
 */
/* global trim */
global.trim = function (str) {
    return str.replace(/(^\s*)|(\s*$)/g, '');
};
/**
 * 分析枚举类型配置值 格式 a:名称1,b:名称2
 * @param str
 * @returns {*}
 */
/* global parse_config_attr */
global.parse_config_attr = function (str, sep = ':') {
    let strs;
    if (str.search(/\r\n/ig) > -1) {
        strs = str.split('\r\n');
    } else if (str.search(/,/ig) > -1) {
        strs = str.split(',');
    } else if (str.search(/\n/ig) > -1) {
        strs = str.split('\n');
    } else {
        strs = [str];
    }
    if (think.isArray(strs)) {
        const obj = {};
        strs.forEach(n => {
            n = n.split(sep);
            obj[n[0]] = n[1];
        });
        return obj;
    }
};
global.parse_type_attr = function (str) {
    let strs;
    if (str.search(/\r\n/ig) > -1) {
        strs = str.split('\r\n');
    } else if (str.search(/,/ig) > -1) {
        strs = str.split(',');
    } else if (str.search(/\n/ig) > -1) {
        strs = str.split('\n');
    } else {
        strs = [str];
    }
    if (think.isArray(strs)) {
        const arr = [];
        for (let v of strs) {
            const obj = {};
            v = v.split(':');
            if (!think.isEmpty(v[0]) && !think.isEmpty(v[1])) {
                obj.id = v[0];
                obj.name = v[1];
                if (obj.id.split('.').length == 1) {
                    obj.pid = 0;
                } else {
                    obj.pid = obj.id.split('.').splice(0, obj.id.split('.').length - 1).join('.');
                }
                arr.push(obj);
            }
        }
        // console.log(arr);
        const tree = arr_to_tree(arr, 0);
        // think.log(tree);
        return tree;
    }
};
/**
 * ltrim()
 * @param str [删除左边的空格]
 * @returns {*|void|string|XML}
 */
/* global ltrim */
global.ltrim = function (str) {
    return str.replace(/(^\s*)/g, '');
};
/**
 *
 * rtrim()
 * @param str [删除右边的空格]
 * @returns {*|void|string|XML}
 */
/* global rtrim */
global.rtrim = function (str) {
    return str.replace(/(\s*$)/g, '');
};
/**
 * 把返回的数据集转换成Tree
 * @param array data 要转换的数据集
 * @param string pid parent标记字段
 * @return array
 */
/* global arr_to_tree */
global.arr_to_tree = function (data, pid) {
    var result = [],
        temp;
    var length = data.length;
    for (var i = 0; i < length; i++) {
        if (data[i].pid == pid) {
            result.push(data[i]);
            temp = arr_to_tree(data, data[i].id);
            if (temp.length > 0) {
                data[i].children = temp;
                data[i].chnum = data[i].children.length;
            }
        }
    }
    return result;
};
// 计算分类信息当前状态
global.sanjiao = arr => {
    var result = [];
    for (var i = 0, len = arr.length; i < len; i++) {
        result.push(result[i - 1] !== undefined ? result[i - 1] + '.' + arr[i] : arr[i]);
    }
    return result;
};
/* global arr_to_tree */
global.sub_cate = function (data, pid) {
    var result = [],
        temp;
    var length = data.length;
    for (var i = 0; i < length; i++) {
        if (data[i].pid == pid) {
            // console.log(data[i]);
            result.push(data[i].id);
            temp = sub_cate(data, data[i].id);
            if (temp.length > 0) {
                result.push(temp.join(','));
            }
        }
    }
    return result;
};
// 获取属性类型信息
/* global get_attribute_type */
global.get_attribute_type = function (type) {
    // TODO 可以加入系统配置
    const _type = {
        'num': ['数字', 'int(10) unsigned NOT NULL'],
        'string': ['字符串', 'varchar(255) NOT NULL'],
        'textarea': ['文本框', 'text NOT NULL'],
        'date': ['日期', 'bigint(13) NOT NULL'],
        'datetime': ['时间', 'bigint(13) NOT NULL'],
        'bool': ['布尔', 'tinyint(2) NOT NULL'],
        'select': ['枚举', 'char(50) NOT NULL'],
        'radio': ['单选', 'char(10) NOT NULL'],
        'checkbox': ['多选', 'varchar(100) NOT NULL'],
        'editor': ['编辑器', 'text NOT NULL'],
        'picture': ['上传图片', 'int(10) unsigned NOT NULL'],
        'file': ['上传附件', 'int(10) unsigned NOT NULL'],
        'suk': ['商品规格', 'text NOT NULL'],
        'pics': ['多图上传', 'varchar(255) NOT NULL'],
        'price': ['价格', 'varchar(255) NOT NULL'],
        'freight': ['运费', 'varchar(255) NOT NULL'],
        'keyword': ['关键词', 'varchar(255) NOT NULL'],
        'relation': ['关联', 'varchar(100) NOT NULL'],
        'atlas': ['图集', 'text NOT NULL']
    };
    return type ? _type[type][0] : _type;
};

/* global array_search */
global.array_search = function (arr, str) {
    // 如果可以的话，调用原生方法
    if (arr && arr.indexOf) {
        return arr.indexOf(str);
    }

    var len = arr.length;
    for (var i = 0; i < len; i++) {
        // 定位该元素位置
        if (arr[i] == str) {
            return i;
        }
    }

    // 数组中不存在该元素
    return false;
};
/* global array_diff */
global.array_diff = function (arr1, arr2) {
    // var arr1 = ["i", "b", "c", "d", "e", "f","x",""]; //数组A
    // var arr2 = ["a", "b", "c", "d", "e", "f", "g"];//数组B
    var temp = []; // 临时数组1
    var temparray = []; // 临时数组2
    for (var i = 0; i < arr2.length; i++) {
        temp[arr2[i]] = true; // 巧妙地方：把数组B的值当成临时数组1的键并赋值为真
    }
    for (var i = 0; i < arr1.length; i++) {
        if (!temp[arr1[i]]) {
            temparray.push(arr1[i]); // 巧妙地方：同时把数组A的值当成临时数组1的键并判断是否为真，如果不为真说明没重复，就合并到一个新数组里，这样就可以得到一个全新并无重复的数组
        }
    }
    // if(think.isEmpty(temparray)){
    //    return
    // }
    return temparray;
};

global.get_list_field = function (data, grid, controller, module) {
    module = module || 'admin';
    // console.log(module);
    const data2 = {};
    let value;

    // 获取当前字段数据
    // console.log(grid);
    for (const field of grid.field) {
        let temp;
        const array = field.split('|'); // TODO
        // console.log(array);
        temp = data[array[0]];
        // console.log(temp);
        // 函数支持
        if (!think.isEmpty(array[1])) {
            temp = call_user_func(array[1], temp);
        }
        data2[array[0]] = temp;
    }
    // console.log(data2);
    if (!think.isEmpty(grid.format)) {
        // value  =   preg_replace_callback('/\[([a-z_]+)\]/', function($match) use($data2){return $data2[$match[1]];}, $grid['format']);
    } else {
        value = data2[Object.keys(data2)];
    }

    // 链接支持
    if (grid.field[0] == 'title' && data.type == '目录') {
        // 目录类型自动设置子文档列表链接
        grid.href = '[LIST]';
    } else if (grid.field[0] == 'title') {
        grid.href = '[EDIT]';
    }

    if (!think.isEmpty(grid.href)) {
        const links = grid.href.split(',');

        const val = [];
        for (const link of links) {
            const array = link.split('|');
            const href = array[0];

            // console.log(href);
            const matches = href.match(/^\[([a-z_]+)\]$/);
            if (matches) {
                val.push(data2[matches[1]]);
                // console.log(val);
            } else {
                const show = !think.isEmpty(array[1]) ? array[1] : value;
                // console.log(show)
                // 替换系统特殊字符串
                const hrefs = {
                    '[DELETE]': 'setstatus/?status=-1&ids=[id]',
                    '[EDIT]': 'edit/?id=[id]&model=[model_id]&cate_id=[category_id]',
                    '[LIST]': 'index/?pid=[id]&model=[model_id]&cate_id=[category_id]'
                };
                const match = hrefs[href].match(/\[(\S+?)\]/g);
                // console.log(match);
                const u = [];
                for (const k of match) {
                    const key = k.replace(/(^\[)|(\]$)/g, '');
                    u.push(data[key]);
                }
                // console.log(u);
                const query = str_replace(match, u, hrefs[href]);
                const href1 = `/${controller}/${query}`;
                // console.log(query);
                if (href == '[DELETE]') {
                    val.push('<a href="' + href1 + '" class="text-info ajax-get confirm">' + show + '</a> ');
                } else {
                    val.push('<a href="' + href1 + '" class="text-info">' + show + '</a> ');
                }
            }
        }
        value = val.join(' ');
    }
    // console.log(value)
    return value;
};

/**
 * 返回一个自定义用户函数给出的第一个参数
 *  call_user_func（回调 函数名， [参数]）
 * @param cb  函数名
 * @param params 数组格式传入参数
 */
/* global call_user_func */
global.call_user_func = function (cb, params) {
    const func = eval(cb);
    if (!think.isArray(params)) {
        params = [params];
    }
    return func.apply(cb, params);
};

/* global str_replace()
 * str_replace(条件[]，替换内容[],被替换的内容)
 * @param search
 * @param replace
 * @param subject
 * @param count
 * @returns {*}
 */
/* global str_replace */
global.str_replace = function (search, replace, subject, count) {
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = r instanceof Array,
        sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? r[j] !== undefined ? r[j] : '' : r[0];
            s[i] = temp.split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
};
// 获取价格格式化
global.get_price_format = function (price, type) {
    const pr = JSON.parse(price);

    if (type == 1) {
        if (think.isNumber(pr.present_price)) {
            pr.present_price = pr.present_price.toString();
        }
        const prices = pr.present_price.split('-');
        let present_price;
        if (prices.length > 1) {
            present_price = formatCurrency(prices[0]) + '-' + formatCurrency(prices[1]);
        } else {
            present_price = formatCurrency(prices[0]);
        }
        price = present_price;
    } else {
        if (pr.discount_price == 0) {
            price = '';
        } else {
            price = formatCurrency(pr.discount_price);
        }
    }
    return price;
};
// 获取价格不格式化
global.get_price = function (price, type) {
    if (price) {
        price = JSON.parse(price);
        if (type == 1) {
            return price.present_price;
        } else {
            if (price.discount_price == 0) {
                return '';
            } else {
                return price.discount_price;
            }
        }
    }
};

/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
/* global formatCurrency */
global.formatCurrency = function (num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) {
        num = '0';
    }
    const sign = num == (num = Math.abs(num));
    num = Math.floor(num * 100 + 0.50000000001);
    let cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) {
        cents = '0' + cents;
    }
    for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    return (sign ? '' : '-') + num + '.' + cents;
};

/**
 * 将数值四舍五入(保留1位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.4'
 * @type String
 */
/* global formatCurrencyTenThou */
global.formatCurrencyTenThou = function (num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) {
        num = '0';
    }
    const sign = num == (num = Math.abs(num));
    num = Math.floor(num * 10 + 0.50000000001);
    const cents = num % 10;
    num = Math.floor(num / 10).toString();
    for (let i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    return (sign ? '' : '-') + num + '.' + cents;
};

/**
 * 构建微信菜单数据结构
 * @param data
 * @returns {{menu: {button: Array}}}
 */
global.createSelfMenu = function (data) {
    const menu = {
        'menu': {
            'button': []
        }
    };
    const button = [];
    for (var i = 0; i < data.length; i++) {
        if (data[i].pid == '0') {
            const item = {
                'id': data[i].id,
                'm_id': data[i].m_id,
                'pid': data[i].pid,
                'type': data[i].type,
                'name': data[i].name,
                'sort': data[i].sort,
                'sub_button': []
            };
            menu.menu.button.push(item);
            button.push(item);
        }
    }
    for (var x = 0; x < button.length; x++) {
        for (var y = 0; y < data.length; y++) {
            if (data[y].pid == button[x].m_id) {
                const sitem = {
                    'type': data[y].type,
                    'm_id': data[y].m_id,
                    'sort': data[y].sort,
                    'name': data[y].name,
                    'url': data[y].url,
                    'media_id': data[y].media_id
                };
                button[x].sub_button.push(sitem);
            }
        }
    }
    return menu;
};

/**
 * 微信创建自定义菜单接口
 * 数据构建
 */

global.buildselfmenu = function (data) {
    const menu = {
        'button': []
    };
    const button = [];
    for (var i = 0; i < data.length; i++) {
        if (data[i].pid == '0') {
            const item = {
                'id': data[i].id,
                'm_id': data[i].m_id,
                'pid': data[i].pid,
                'type': data[i].type,
                'name': data[i].name,
                'sort': data[i].sort,
                'sub_button': []
            };
            menu.menu.button.push(item);
            button.push(item);
        }
    }
    for (var x = 0; x < button.length; x++) {
        for (var y = 0; y < data.length; y++) {
            if (data[y].pid == button[x].m_id) {
                const sitem = {
                    'type': data[y].type,
                    'sort': data[y].sort,
                    'name': data[y].name,
                    'url': data[y].url,
                    'media_id': data[y].media_id
                };
                button[x].sub_button.push(sitem);
            }
        }
    }
    return menu;
};

/**
 * 验证是否为智能手机
 * @ param {string} data :this.userAgent;
 * @ return {bool}
 */
/** global checkMobile */
global.checkMobile = function (agent) {
    let flag = false;
    agent = agent.toLowerCase();
    const keywords = ['android', 'iphone', 'ipod', 'ipad', 'windows phone', 'mqqbrowser'];

    // 排除 Windows 桌面系统
    if (!(agent.indexOf('windows nt') > -1) || agent.indexOf('windows nt') > -1 && agent.indexOf('compatible; msie 9.0;') > -1) {
        // 排除苹果桌面系统
        if (!(agent.indexOf('windows nt') > -1) && !agent.indexOf('macintosh') > -1 && !(agent.indexOf('ipad') > -1)) {
            for (const item of keywords) {
                if (agent.indexOf(item) > -1) {
                    flag = true;
                    break;
                }
            }
        }
    }
    return flag;
};
/**
 * 验证时否是微信
 *
 */
global.is_weixin = agent => {
    let flag = false;
    agent = agent.toLowerCase();
    // let key = ["mqqbrowser","micromessenger"];
    const key = ['micromessenger'];
    // 排除 Windows 桌面系统
    if (!(agent.indexOf('windows nt') > -1) || agent.indexOf('windows nt') > -1 && agent.indexOf('compatible; msie 9.0;') > -1) {
        // 排除苹果桌面系统
        if (!(agent.indexOf('windows nt') > -1) && !agent.indexOf('macintosh') > -1) {
            for (const item of key) {
                if (agent.indexOf(item) > -1) {
                    flag = true;
                    break;
                }
            }
        }
    }
    return flag;
};
/**
 *
 * @param time
 * @returns {string}'January 31, 2018 15:03:26'
 */
global.date_from = time => {
    // January 31, 2018 15:03:26
    const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    const d = new Date(time);
    const month = months[d.getMonth()];
    const day = d.getDate();
    const year = d.getFullYear();
    const hour = d.getHours() < 10 ? `0${d.getHours()}` : d.getHours();
    const min = d.getMinutes() < 10 ? `0${d.getMinutes()}` : d.getMinutes();
    const sec = d.getSeconds() < 10 ? `0${d.getSeconds()}` : d.getSeconds();
    const res = `${month} ${day}, ${year} ${hour}:${min}:${sec}`;
    return res;
};

global.image_view = (str, w, m) => {
    // console.log(info);
    const imgReg = /<img.*?(?:>|\/>)/gi;
    const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
    const arr = str.match(imgReg);
    if (!think.isEmpty(arr)) {
        const narr = [];
        for (const img of arr) {
            const _img = img.match(srcReg);
            // console.log(_img);
            const nimg = _img[1] + '?imageView2/' + m + '/w/' + w;
            // console.log(nimg)
            const inputimg = _img['input'].replace(_img[1], nimg);
            narr.push(inputimg);
        }
        return str_replace(arr, narr, str);
    } else {
        return str;
    }
};
global.img_text_view = (str, w, h) => {
    // console.log(info);
    const imgReg = /<img.*?(?:>|\/>)/gi;
    const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
    if (think.isEmpty(str)) {
        return [];
    }
    const arr = str.match(imgReg);
    if (!think.isEmpty(arr)) {
        const narr = [];
        for (const img of arr) {
            const _img = img.match(srcReg);
            // console.log(_img[1]);
            let nimg = _img[1];
            if (!think.isEmpty(w) && !think.isEmpty(h)) {
                nimg = _img[1] + '?imageView2/1/w/' + w + '/h/' + h;
            }
            // console.log(nimg)
            narr.push(nimg);
        }
        // console.log(narr);
        return narr;
    } else {
        return [];
    }
};

// 生成6位的随机数
global.MathRand = function () {
    var Num = '';
    for (var i = 0; i < 6; i++) {
        Num += Math.floor(Math.random() * 10);
    }
    return Num;
};

global.GetDateStr = function (AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期，不足10补0
    var d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
    return y + '-' + m + '-' + d;
};
// 转意符换成普通字符
global.escape2Html = function (str) {
    var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
        return arrEntities[t];
    });
};
global.html_decode = function (str) {
    var s = '';
    if (str.length == 0) return '';
    s = str.replace(/&gt;/g, '&');
    s = s.replace(/&lt;/g, '<');
    s = s.replace(/&gt;/g, '>');
    s = s.replace(/&nbsp;/g, ' ');
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, '"');
    s = s.replace(/<br>/g, '\n');
    return s;
};

global.html_encode = function (str) {
    var s = '';
    if (str.length == 0) return '';
    s = str.replace(/&/g, '&gt;');
    s = s.replace(/</g, '&lt;');
    s = s.replace(/>/g, '&gt;');
    s = s.replace(/ /g, '&nbsp;');
    s = s.replace(/\'/g, '&#39;');
    s = s.replace(/\"/g, '&quot;');
    s = s.replace(/\n/g, '<br>');
    return s;
};
/**
 * 检查pos(推荐位的值)是否包含指定推荐位contain
 * @param number pos 推荐位的值
 * @param number contain 指定推荐位
 * @return boolean true 包含 ， false 不包含
 */
global.check_document_position = (pos = 0, contain = 0) => {
    if (think.isEmpty(pos) || think.isEmpty(contain)) {
        return false;
    }
    // 将两个参数进行按位与运算，不为0则表示$contain属于$pos
    const res = pos & contain;
    if (res !== 0) {
        return true;
    } else {
        return false;
    }
};